//
// Created by wujehy on 2020/9/23.
//

//#include <baseType.h>
#include <string>
#include "BaseDartAPI.h"
//#include <glog/logging.h>
Dart_PostCObjectType dartPostCObject = NULL;
Dart_Port listerner_port= 0 ;
void RegisterDart_PostCObject(Dart_PostCObjectType _dartPostCObject) {
//    LOGINFO<<" RegisterDart_PostCObject init";
//    debug_console_log("RegisterDart_PostCObject init");
    dartPostCObject = _dartPostCObject;
}

void RegisterListernerDartPort(Dart_Port callbackPort)
{
//    LOGINFO<<" RegisterDartPort port = " << callbackPort;

    listerner_port = callbackPort;
}

Dart_Port getListernerPort()
{
    return listerner_port;
}

bool callDartCallBack(Dart_Port callbackPort ,  Dart_CObject *dart_object)
{
//    LOGINFO<<" RegisterDart_PostCObject call ";
//    debug_console_log("RegisterDart_PostCObject call");
    if (dartPostCObject != NULL) {
//        LOGINFO<<" RegisterDart_PostCObject into  callbackPort = " << callbackPort;
        std::string coutStr = "RegisterDart_PostCObject into  callbackPort =" + std::to_string(callbackPort);

//        debug_console_log(coutStr.c_str());
        return dartPostCObject(callbackPort , dart_object);
    } else
    {
//        LOGINFO<<" RegisterDart_PostCObject not found  ";
        warn_console_log("RegisterDart_PostCObject not found");
        return false;
    }
}


bool callDartCallBackNoLog(Dart_Port callbackPort ,  Dart_CObject *dart_object)
{
//    LOGINFO<<" RegisterDart_PostCObject call ";
    debug_console_log("RegisterDart_PostCObject call");
    if (dartPostCObject != NULL) {
//        LOGINFO<<" RegisterDart_PostCObject into  callbackPort = " << callbackPort;
        std::string coutStr = "RegisterDart_PostCObject into  callbackPort =" + std::to_string(callbackPort);
        warn_console_log(coutStr.c_str());

        return dartPostCObject(callbackPort , dart_object);
    } else
    {
//        LOGINFO<<" RegisterDart_PostCObject not found  ";

        warn_console_log("RegisterDart_PostCObject not found");
        return false;
    }
}

void callDartCallBackJson(Dart_Port callbackPort, const char *json)
{
    Dart_CObject dart_object;
    dart_object.type = Dart_CObject_kString;
    dart_object.value.as_string = const_cast<char *>(json);
    
    bool result = callDartCallBack(callbackPort, &dart_object);
//    LOGINFO << " will to callback end callbackPort = " << callbackPort;
    debug_console_log(" will to callback end callbackPort = ");
    if (!result)
    {
//        LOGINFO << " will to callback false " ;
        warn_console_log("will to callback false");
    }

}

void callDartCallbackData(Dart_Port callbackPort , int code , const std::string &msg , const std::string &json)
{
    Dart_CObject dart_object;
    dart_object.type= Dart_CObject_kArray;
    dart_object.value.as_array.length = 3 ;
//    dart_object.value.as_array.values =

    Dart_CObject dartCObjectCode ;
    dartCObjectCode.type = Dart_CObject_kInt32 ;
    dartCObjectCode.value.as_int32 = code;

    //
    Dart_CObject dartCObjectMsg ;
    dartCObjectMsg.type = Dart_CObject_kString ;
    dartCObjectMsg.value.as_string = (char*)msg.c_str();
//
    Dart_CObject dartCObjectJson ;
    dartCObjectJson.type = Dart_CObject_kTypedData ;
    dartCObjectJson.value.as_typed_data.type = Dart_TypedData_kUint8;
    dartCObjectJson.value.as_typed_data.length = json.length();
    dartCObjectJson.value.as_typed_data.values = (uint8_t *)json.c_str();
//
    _Dart_CObject* dartCObjects[] = {&dartCObjectCode , &dartCObjectMsg , &dartCObjectJson };
//
//    dartCObjectCode.value.as_array.values = dartCObjects;
    dart_object.value.as_array.values = dartCObjects ;
//    dart_object.value.as_array.values[1] = &dartCObjectJson ;
    bool result = callDartCallBack(callbackPort, &dart_object);
//    debug_console_log(" will to callback end callbackPort = ");
    if (!result)
    {
        warn_console_log("will to callback false");
    }

}

void callDartCallBackJsonNoLog(Dart_Port callbackPort, const char *json)
{
    Dart_CObject dart_object;
    dart_object.type = Dart_CObject_kString;
    dart_object.value.as_string = const_cast<char *>(json);
    callDartCallBackNoLog(callbackPort, &dart_object);
}
